/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exercises.day05.pt02.fileio;

import exercises.day04.pt01.bean.AccountRegistration;
import exercises.day04.pt01.bean.Account_Holder;
import exercises.day04.pt01.bean.Account_Holder_Transaction;
import exercises.day04.pt01.dao.Account_Registration_DAO;
import exercises.day04.pt02.Exception.AHBalanceIsNotNumericException;
import exercises.day04.pt02.Exception.AHBalanceIsNullException;
import exercises.day04.pt02.Exception.AHIdIsNullException;
import exercises.day04.pt02.Exception.AHTIDIsNotNumericException;
import exercises.day04.pt02.Exception.AccountTypeIsInvalidException;
import exercises.day04.pt02.Exception.AccountTypeIsNullException;
import exercises.day04.pt02.Exception.AddressIsNullException;
import exercises.day04.pt02.Exception.AddressLenghtLessThan25Exception;
import exercises.day04.pt02.Exception.AnnualIncomeIsNullException;
import exercises.day04.pt02.Exception.AnnualIncomeValidationException;
import exercises.day04.pt02.Exception.FirstNameIsNullException;
import exercises.day04.pt02.Exception.IncomeIsNotDoubleException;
import exercises.day04.pt02.Exception.LastNameIsNullException;
import exercises.day04.pt02.Exception.NameIsJohnDoeException;
import exercises.day04.pt02.Exception.PhoneIsNullException;
import exercises.day04.pt02.Exception.PhoneLenghtLessThan7Exception;
import exercises.day04.pt02.Exception.RegisterIdIsNotIntegerException;
import java.io.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Student
 */
public class ReadWrite {
    
    public AccountRegistration readSave(String path) {
        
        AccountRegistration ar = null;
        Account_Holder ah = null;
        Account_Holder_Transaction aht = null;
        
        String line = null;
        String[] s;
        {
            FileReader reader = null;
            try {
                File file = new File(path);
                reader = new FileReader(file);
                BufferedReader scan = new BufferedReader(reader);
                line = scan.readLine();
                while ((line = scan.readLine()) != null) {
                    s = line.split(",");
                    
                    for (int a = 0; a < s.length; a++) {
                        s[a] = s[a].trim();
                        
                    }
                    
                    
                    ar = new AccountRegistration(s[0], s[1], s[2], s[3] + "," + s[4], s[5], s[6], s[7]);
                    //    ah = new Account_Holder(s[7], ar.getRegId(), s[8]);
                    //    aht = new Account_Holder_Transaction(s[9], ah.getAhID(), s[10], s[11], ah.getBalance());

                    Account_Registration_DAO rdao = new Account_Registration_DAO();
                    //    Account_Holder_DAO hdao = new Account_Holder_DAO();
                    //    Account_Holder_Transaction_DAO ahdao = new Account_Holder_Transaction_DAO();

                    rdao.register(ar);
                    //         hdao.register(ah);
                    //         ahdao.log(aht);

                }
                scan.close();
            } catch (AccountTypeIsInvalidException ex) {
                JOptionPane.showMessageDialog(null, ex.toString());
            } catch (AccountTypeIsNullException ex) {
                JOptionPane.showMessageDialog(null, ex.toString());
            } catch (AddressIsNullException ex) {
                JOptionPane.showMessageDialog(null, ex.toString());
            } catch (AddressLenghtLessThan25Exception ex) {
                JOptionPane.showMessageDialog(null, ex.toString());
            } catch (FirstNameIsNullException ex) {
                JOptionPane.showMessageDialog(null, ex.toString());
            } catch (LastNameIsNullException ex) {
                JOptionPane.showMessageDialog(null, ex.toString());
            } catch (NameIsJohnDoeException ex) {
                JOptionPane.showMessageDialog(null, ex.toString());
            } catch (AnnualIncomeValidationException ex) {
                JOptionPane.showMessageDialog(null, ex.toString());
            } catch (PhoneIsNullException ex) {
                JOptionPane.showMessageDialog(null, ex.toString());
            } catch (PhoneLenghtLessThan7Exception ex) {
                JOptionPane.showMessageDialog(null, ex.toString());
            } catch (RegisterIdIsNotIntegerException ex) {
                JOptionPane.showMessageDialog(null, ex.toString());
            } catch (IncomeIsNotDoubleException ex) {
                JOptionPane.showMessageDialog(null, ex.toString());
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, ex.toString());
            } catch (AnnualIncomeIsNullException ex) {
                JOptionPane.showMessageDialog(null, ex.toString());
            } finally {
                try {
                    reader.close();
                } catch (IOException ex) {
                    Logger.getLogger(ReadWrite.class.getName()).log(Level.SEVERE, null, ex);
                }
                
            }
        }
        
        
        return ar;
    }
    
    public File searchAccountType(String accountType, String path) {
        Account_Registration_DAO ardao = new Account_Registration_DAO();
        
        ArrayList<AccountRegistration> result = new ArrayList<AccountRegistration>();
        
        result = ardao.searchAccountType(accountType);
        
        
        File file = new File("results");

        //write to file results
        try {
            
            FileWriter writer = new FileWriter(path);
            
            
            for (AccountRegistration a : result) {
                
                System.out.println(a);
                writer.write(a.toString() + System.getProperty("line.separator"));
                
                
                
                
            }
            
            if (result.size() == 0) {
                System.out.println("no records found for " + accountType);
            }
            
            writer.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        //set the found to null
        result = null;
        
        
        
        return file;
        
        
        
        
    }
}
